package com.zhuoli.zblog.controller;

import com.zhuoli.zblog.repository.model.User;
import com.zhuoli.zblog.service.UserService;
import jodd.util.StringUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.autoconfigure.EnableAutoConfiguration;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.security.authentication.AuthenticationManager;
import org.springframework.security.authentication.UsernamePasswordAuthenticationToken;
import org.springframework.security.core.Authentication;
import org.springframework.security.core.context.SecurityContextHolder;
import org.springframework.security.crypto.bcrypt.BCryptPasswordEncoder;
import org.springframework.security.web.authentication.WebAuthenticationDetails;
import org.springframework.security.web.context.HttpSessionSecurityContextRepository;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

/**
 * Author: zhuoli
 * Description:
 * Date: 下午5:21 2018/4/23
 */
@RestController
@EnableAutoConfiguration
public class RegisterController {
    @Autowired
    private UserService userService;

    @Autowired
    protected AuthenticationManager authenticationManager;

    @Autowired
    private  HttpServletRequest httpServletRequest;

    @Autowired
    HttpServletResponse httpServletResponse;
    
    private String userName = "";
    
    private String userPwd = "";

    @RequestMapping(value = "/user_register", method = RequestMethod.POST)
    public ResponseEntity registerUser(@RequestParam("user_phone") String userPhone,
                                       @RequestParam("user_pwd") String userPwd) {

        if (StringUtil.isEmpty(userPhone) || StringUtil.isEmpty(userPwd)) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(null);
        }
        try {
            userName = userPhone;
            this.userPwd = userPwd;

            BCryptPasswordEncoder encoder = new BCryptPasswordEncoder();
            String pwdEncode = encoder.encode(userPwd);

            User user = new User();
            user.setUserPhone(userPhone);
            user.setUserPwd(pwdEncode);
            userService.saveUser(user);

            redirctIndex(httpServletRequest);
            return ResponseEntity.ok("register success");
        } catch (Exception e) {
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(e.getStackTrace());
        }
    }


    /**
     * @Description: 完成注册成功直接登陆功能，模拟token
     * @Author: zhuoli
     * @Description:
     * @param request
     * @Description:
     * @Date: 上午6:44 2018/4/27
     */
    public void redirctIndex(HttpServletRequest request){

        UsernamePasswordAuthenticationToken token=new UsernamePasswordAuthenticationToken(userName,userPwd);
        token.setDetails(new WebAuthenticationDetails(request));
        Authentication authenticatedUser=authenticationManager.authenticate(token);
        SecurityContextHolder.getContext().setAuthentication(authenticatedUser);
        request.getSession().setAttribute(HttpSessionSecurityContextRepository.SPRING_SECURITY_CONTEXT_KEY, SecurityContextHolder.getContext());}

}
